package com.yifan.servlet;

import com.alibaba.fastjson.JSON;
import com.yifan.entity.User;
import com.yifan.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends HttpServlet{
	
	UserService userService = new UserService();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		//获取请求的方法
		String method = request.getParameter("method");
		//请求分发
		if("getUsers".equals(method)){
			getUsers(request, response);
		}else if("getUser".equals(method)) {
			getUser(request, response);
		}else if("getUser2".equals(method)) {
			getUser2(request, response);
		}else if("updateUser".equals(method)) {
			updateUser(request, response);
		}else if("addUser".equals(method)) {
			addUser(request, response);
		}else if("deleteUser".equals(method)) {
			deleteUser(request, response);
		}else if("deleteUsers".equals(method)) {
			deleteUsers(request, response);
		}else if("updateUserByU_P".equals(method)) {
			updateUserByU_P(request, response);
		}

	}

	private void getUsers(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		response.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String realname = request.getParameter("realname");
		String sex = request.getParameter("sex");
		//获取分页参数
		int page = 1;//Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = 78;//Integer.parseInt(request.getParameter("pageSize"));
		
		Map<String, Object> users = userService.getUsers(username, realname, sex, page, pageSize);
		// response.getWriter().write(JSON.toJSON(users).toString());
		request.setAttribute("users", users.get("data"));
		request.getRequestDispatcher("/user.jsp").forward(request,response);
	}
	
	private void getUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		//int id = (int) request.getSession().getAttribute("userid");
		User user = userService.getUser(id);
		System.out.println(JSON.toJSON(user));
		response.getWriter().write(JSON.toJSON(user).toString());
	}
	
	private void getUser2(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		User user = userService.getUser(id);
		request.getSession().setAttribute("fsuser", user);
		request.getSession().setAttribute("msgType", request.getParameter("flag"));
		
		response.sendRedirect("liuyan.jsp");
	}
	
	private void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String realname = request.getParameter("realname");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		String userType = request.getParameter("userType");
		User user = new User(id, username, password, realname, sex, phone, userType);
		Map<String, String> map = userService.updateUser(user);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void updateUserByU_P(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String[] obj = ((String)request.getSession().getAttribute("u_p")).split(",");
		String username = obj[0];
		String password = obj[1];
		String old_password = request.getParameter("old_password");
		String new_password = request.getParameter("new_password");
		Map<String, String> map = userService.updateUserByU_P(username, password, old_password, new_password);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		
		Map<String, String> map = userService.deleteUser(id);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void deleteUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		String[] idarr = ids.substring(0, ids.length()-1).split(",");
		System.out.println(idarr.toString());
		Map<String, String> map = userService.deleteUsers(idarr);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void addUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String realname = request.getParameter("realname");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		String userType = request.getParameter("userType");
		User user = new User(0, username, password, realname, sex, phone, userType);
		Map<String, String> map = userService.addUser(user);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
}
